/*
 * Hero.cpp
 *
 *  Created on: 01/12/2011
 *      Author: arleudo
 */

#include "objetos/Hero.h"

namespace objetos
{
	//--------------------------------------------------------------------------------
	Hero::Hero( sf::Sprite* target )
			: Object( target )
	{
		setJumpForce( 18 );
		accel = 2;
		pushPower = 0.5;

		isRight = true;
		setHitPoints( 1000 );
	}
	//--------------------------------------------------------------------------------
	Hero::~Hero()
	{

	}
	//--------------------------------------------------------------------------------
	void Hero::colisionHandler( Object* target )
	{
		vel.x = 0;
		accel = 0;
		if ( isRight )
		{
			getSprite()->SetPosition( getSprite()->GetPosition().x - 2.5,
					getSprite()->GetPosition().y );
			if ( !isJumping() )
			{
				target->getSprite()->SetPosition( target->getSprite()->GetPosition().x + pushPower,
						target->getSprite()->GetPosition().y );
			}
		}
		if ( !isRight )
		{
			getSprite()->SetPosition( getSprite()->GetPosition().x + 2.5,
					getSprite()->GetPosition().y );
			if ( !isJumping() )
			{
				target->getSprite()->SetPosition( target->getSprite()->GetPosition().x - pushPower,
						target->getSprite()->GetPosition().y );
			}
		}
	}
	//--------------------------------------------------------------------------------
	void Hero::load()
	{
		std::string sourcePath = "resources/ciclope/";
		std::string sourceExtension = ".png";

		std::string str = "Static";
		Asset::getInstance()->loadImages( sourcePath, str, sourceExtension, 17, vecStatic );

		str = "Right";
		Asset::getInstance()->loadImages( sourcePath, str, sourceExtension, 17, vecRight );

		str = "left";
		Asset::getInstance()->loadImages( sourcePath, str, sourceExtension, 17, vecLeft );

		str = "Attack";
		Asset::getInstance()->loadImages( sourcePath, str, sourceExtension, 8, vecAttacking );

		str = "Shotting";
		Asset::getInstance()->loadImages( sourcePath, str, sourceExtension, 3, vecShotting );

		setY( 400 );

		Animation* A_Static = new Animation( vecStatic, 0.1 );
		Animation* A_RunnerRight = new Animation( vecRight, 0.05 );
		Animation* A_RunnerLeft = new Animation( vecLeft, 0.05 );
		Animation* A_Blasting = new Animation( vecAttacking, 0.045 );
		Animation* A_Attacking = new Animation( vecShotting, 0.2 );

		addAnimation( A_Static );
		addAnimation( A_RunnerRight );
		addAnimation( A_RunnerLeft );
		addAnimation( A_Attacking );
		addAnimation( A_Blasting );
	}
	//--------------------------------------------------------------------------------
	void Hero::move( void )
	{

	}
	//--------------------------------------------------------------------------------
	void Hero::runAnimation( void )
	{
		bool apertou = false;

		if ( IS_KEY_DOWN(D) )
		{
			this->setImage( getAnimation().at( 1 )->run() );
			apertou = true;
		}
		if ( IS_KEY_DOWN(A) )
		{
			this->setImage( getAnimation().at( 2 )->run() );
			apertou = true;
		}
		if ( IS_KEY_DOWN(X) )
		{
			this->setImage( getAnimation().at( 1 )->run() );
			apertou = true;
		}
		if ( IS_KEY_DOWN(S) )
		{
			this->setImage( getAnimation().at( 3 )->run() );
			apertou = true;
		}
		if ( IS_KEY_DOWN(Q) )
		{
			this->setImage( getAnimation().at( 4 )->run() );
			apertou = true;
		}

		if ( !apertou || ( IS_KEY_DOWN(A) && IS_KEY_DOWN(D) ) )
		{
			this->setImage( getAnimation().at( 0 )->run() );
		}
	}
	//--------------------------------------------------------------------------------
	void Hero::update( void )
	{
		accel = 2;
		vel.y += World::getInstance()->getGravity();
		bool apertou = false;

		//acelerando
		if ( IS_KEY_DOWN(D) )
		{
			isRight = true;
			apertou = true;
			vel.x += accel;
			if ( vel.x > MAX_VELOCITY )
			{
				vel.x = MAX_VELOCITY;
			}
		}

		if ( IS_KEY_DOWN(A) )
		{
			isRight = false;
			apertou = true;
			vel.x -= accel;
			if ( vel.x < MAX_MINIM )
			{
				vel.x = MAX_MINIM;
			}
		}

		//limitando o chao
		if ( getSprite()->GetPosition().y > 400 )
		{
			getSprite()->SetPosition( getSprite()->GetPosition().x, 400 );
			vel.y = 0;
			setJumping( false );
		}

		if ( IS_KEY_DOWN(X) && !isJumping() )
		{
			vel.y = -getJumpForce();
			setJumping( true );
		}
		if ( !apertou || ( IS_KEY_DOWN(D) && IS_KEY_DOWN(A) ) )
		{
			vel.x = 0;
		}

		getSprite()->Move( vel );

		runAnimation();
		//move();
	}

} /* namespace objetos */
